package org.jeecg.modules.workflow.convert.change.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boc.ewr.bcimp.entity.Pro;
import com.boc.ewr.bcimp.service.IProService;
import com.boc.ewr.business.entity.SnorgChangeApply;
import org.aspectj.lang.annotation.Pointcut;
import org.jeecg.common.aspect.annotation.AutoDict;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.workflow.vo.change.org.BpmChgSnorgCreateReqVo;
import org.jeecg.common.workflow.vo.change.org.BpmChgSnorgRespVo;
import org.jeecg.modules.workflow.convert.change.BpmChgSnorgConvert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : Wzz
 * @create : 2023-04- 26- 15:02
 * @desc :
 */
@Service
public class BpmChgSnorgConvertImpl implements BpmChgSnorgConvert {

    @Autowired
    private IProService proService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Override
    public SnorgChangeApply convert(BpmChgSnorgCreateReqVo bean) {
        if (bean == null) {
            return null;
        }
        SnorgChangeApply snorgChangeApply = new SnorgChangeApply();
        BeanUtils.copyProperties(bean, snorgChangeApply);
        snorgChangeApply.setApplyDate(new Date());

        return snorgChangeApply;
    }

    @Override
    public BpmChgSnorgRespVo convert(SnorgChangeApply bean) {
        if (bean == null) {
            return null;
        }

        BpmChgSnorgRespVo bpmChgSnorgRespVo = new BpmChgSnorgRespVo();

        BeanUtils.copyProperties(bean, bpmChgSnorgRespVo);
        bpmChgSnorgRespVo.setCreateTime(bean.getApplyDate());

        return bpmChgSnorgRespVo;
    }

    @Override
    public BpmChgSnorgRespVo getInfoByProtocolNo(String protocolNo) {
        BpmChgSnorgRespVo bpmChgSnorgRespVo = new BpmChgSnorgRespVo();

        List<Pro> proList = proService.list(
                new LambdaQueryWrapper<Pro>()
                        .eq(Pro::getCaProtocolNo, protocolNo)
        );

        if (proList.size() > 0) {
            Pro pro = proList.get(0);
            bpmChgSnorgRespVo.setCusmNo(pro.getCusmNo());
            bpmChgSnorgRespVo.setMainAcctNo(pro.getMainAcctNo());
            bpmChgSnorgRespVo.setCusmNm(pro.getCusmName());
            bpmChgSnorgRespVo.setOrgBranchNo(pro.getSignedOrg());
            bpmChgSnorgRespVo.setAdminBranchNm(
                    sysBaseAPI.selectAllById(sysBaseAPI.getParentDepartId(pro.getSignedOrg()).getText()).getDepartName()
            );
        } else {
            bpmChgSnorgRespVo.setCusmNo("0000000000");
            bpmChgSnorgRespVo.setMainAcctNo("0000000000");
            bpmChgSnorgRespVo.setCusmNm("未根据协议号查询到相关信息！");
            bpmChgSnorgRespVo.setOrgBranchNo("00000");
            bpmChgSnorgRespVo.setAdminBranchNm("未根据协议号查询到相关信息！");
        }

        return bpmChgSnorgRespVo;
    }

    @Override
    public List<BpmChgSnorgRespVo> convertList(List<SnorgChangeApply> list) {

        if (list == null) {
            return null;
        }

        List<BpmChgSnorgRespVo> list1 = new ArrayList<BpmChgSnorgRespVo>(list.size());
        for (SnorgChangeApply snorgChangeApply : list) {
            list1.add(convert(snorgChangeApply));
        }

        return list1;
    }

    @Override
    public IPage<BpmChgSnorgRespVo> convertPage(IPage<SnorgChangeApply> page) {
        if (page == null) {
            return null;
        }

        IPage<BpmChgSnorgRespVo> pageResult = new Page<>();

        pageResult.setRecords(convertList(page.getRecords()));
        pageResult.setTotal(page.getTotal());

        return pageResult;
    }
}
